package leetcode.editor.cn;

//给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。 
//
// 
//
// 示例 1： 
//
// 
//输入：timePoints = ["23:59","00:00"]
//输出：1
// 
//
// 示例 2： 
//
// 
//输入：timePoints = ["00:00","23:59","00:00"]
//输出：0
// 
//
// 
//
// 提示： 
//
// 
// 2 <= timePoints <= 2 * 10⁴ 
// timePoints[i] 格式为 "HH:MM" 
// 
// Related Topics 数组 数学 字符串 排序 👍 107 👎 0

import java.util.Arrays;
import java.util.List;

//Java：最小时间差
public class MinimumTimeDifference_539{
    public static void main(String[] args) {
        Solution solution = new MinimumTimeDifference_539().new Solution();
        // TO TEST
    }
    
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int findMinDifference(List<String> timePoints) {
        int length = timePoints.size(), total = 24*60;
        int[] timeArray = new int[length];
        for (int i = 0; i < length; i++) {
            timeArray[i] = time2int(timePoints.get(i));
        }
        Arrays.sort(timeArray);
        int top = timeArray[length-1]-timeArray[0];
        int min = Math.min(top,total - top);
        for (int i = 1; i < length; i++) {
            int diff = timeArray[i]-timeArray[i-1];
            min = Math.min(min,diff);
        }
        return min;
    }
    public int time2int(String str){
        int hh = Integer.parseInt(str.substring(0,2));
        int mm = Integer.parseInt(str.substring(3,5));
        return hh*60+mm;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
